package com.liesheng.haylou.ui.main;


import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.liesheng.haylou.R;
import com.liesheng.haylou.base.BaseActivity;
import com.liesheng.haylou.base.BaseFragment;
import com.liesheng.haylou.base.BaseVm;
import com.liesheng.haylou.bean.UserUpdateBean;
import com.liesheng.haylou.bluetooth.BleManager;
import com.liesheng.haylou.databinding.ActivityMainBinding;
import com.liesheng.haylou.net.HttpCallback;
import com.liesheng.haylou.ui.main.equipment.EquipmentFragment;
import com.liesheng.haylou.ui.main.content.ContentFragment;
import com.liesheng.haylou.ui.main.home.HomeFragment;
import com.liesheng.haylou.utils.ToastUtil;
import com.liesheng.haylou.utils.sp.SpUtil;
import com.liesheng.haylou.view.dialog.ConfirmDialog;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends BaseActivity<ActivityMainBinding, BaseVm> {
    private String[] tabText = {"首页", "内容", "设备"};
    //未选中icon
    private int[] normalIcon = {R.drawable.tab_ico_chart_nor, R.drawable.tab_ico_content_nor, R.drawable.tab_ico_equ_nor};
    //选中时icon
    private int[] selectIcon = {R.drawable.tab_ico_chart_pressed, R.drawable.tab_ico_content_pressed, R.drawable.tab_ico_equ_pressed};

    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected ActivityMainBinding getmBinding() {
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_main, null, false);
        return mBinding;
    }


    @Override
    protected BaseVm getViewModel() {
        return null;
    }

    @Override
    protected void initData() {
        UserInfo();
        showHeadLayout(false);
        setStatusBarColor(R.color.main_bg, false);
        fragments.add(new HomeFragment());
        fragments.add(new ContentFragment());
        fragments.add(new EquipmentFragment());

        mBinding.navigationBar.titleItems(tabText)
                .normalIconItems(normalIcon)
                .selectIconItems(selectIcon)
                .fragmentList(fragments)
                .fragmentManager(getSupportFragmentManager())
                .canScroll(true)
                .build();

        checkLocationOpen();
    }

    /**
     * 获取用户信息
     */
    private void UserInfo() {
        requestHttp(buildHttpService().getUserInfo(), new HttpCallback<UserUpdateBean>() {
            @Override
            public void onNext(UserUpdateBean userUpdateBean) {
                if (null != userUpdateBean) {
                    if (userUpdateBean.getCode() == 200) {
                        if(userUpdateBean.getData() != null){
                            SpUtil.saveUserInfo(userUpdateBean);
                            SpUtil.saveNickName(userUpdateBean.getData().getNickName());
                            SpUtil.saveAvatar(userUpdateBean.getData().getAvatar());
                        }

                    }
                }
            }
        });
    }
    public static void startBy(BaseActivity activity) {
        Intent intent = new Intent(activity, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }

    @Override
    public void onClickView(View view) {
        ((BaseFragment) fragments.get(mBinding.navigationBar.getmViewPager().getCurrentItem())).onClickView(view);
    }


    /**
     * 监听Back键按下事件,方法1:
     * 当前Activity.
     */
    private long mExitTime;

    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            ToastUtil.showTosat(this, "再按一次退出程序");
            mExitTime = System.currentTimeMillis();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BleManager.getInstance().closeAllConn();
    }

    @Override
    protected void openBluetooth(boolean isOpen) {
        super.openBluetooth(isOpen);
        Fragment fragment = fragments.get(mBinding.navigationBar.getmViewPager().getCurrentItem());
        if (fragment instanceof EquipmentFragment)
            ((EquipmentFragment) fragment).openBluetooth(isOpen);

    }

    //引导用户开启位置服务 , 后台扫描蓝牙必须要开启位置服务
    private void checkLocationOpen() {
        LocationManager locationManager
                = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        boolean isLocationOpen = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        if (!isLocationOpen) {

            ConfirmDialog.newInstance().setMessage(R.string.open_location)
                    .setSubmitText(R.string.open)
                    .setSubmitOnclickListener(new ConfirmDialog.SubmitOnclickListener() {
                        @Override
                        public void onSubmit() {
                            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            startActivityForResult(intent, 1);
                        }
                    }).show(getSupportFragmentManager());
        }
    }
}
